﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Core.Models;

namespace Core
{
    public static class BUHelper
    {

        public static void PopulatePlugins(CoreEntities core, ref BusinessUnit bu, bool IsReadOnly)
        {
            string PluginIds = "";
            string PluginDisplay = "";
            foreach (Core.Models.BusinessUnitPlugin buplugin in bu.BusinessUnitPlugins.Where(m => (m.IsDroppedYN == "N")))
            {
                PluginIds += "," + buplugin.PluginId.ToString();
                /* if the formatting below is changed, the changes need to be cascaded to the edit & create views as well. */
                if (buplugin.Plugin.Name == "Core" || IsReadOnly)
                    PluginDisplay += "&nbsp;&lt;<span>" + buplugin.Plugin.Name + "</span>&gt;";
                else
                    PluginDisplay += "&nbsp;&lt;<span>" + buplugin.Plugin.Name + "&nbsp;<a href='javascript: RemovePlugin(\"" + buplugin.PluginId.ToString() + "\")'>X</a></span>&gt;";

            }
            /* In case of create there are no plugins associated, so make sure that Core gets added */
            if (String.IsNullOrEmpty(PluginIds))
            {
                Core.Models.Plugin p = core.Plugins.First(m => m.Name == "Core");
                PluginIds += "," + p.PluginId.ToString();
                PluginDisplay += "&nbsp;&lt;<span>" + p.Name + "</span>&gt;";
            }
            bu.PluginIds = PluginIds;
            bu.PluginDisplay = PluginDisplay;
        }

        public static void UpdatePlugins(ref CoreEntities core, Guid buid, string pluginids)
        {
            if (String.IsNullOrEmpty(pluginids)) return;
            List<string> plugins = pluginids.Split((",").ToCharArray(), 9999).ToList();
            List<Guid> PluginIds = new List<Guid>();
            foreach (string pluginid in plugins)
                if (!String.IsNullOrEmpty(pluginid)) PluginIds.Add(new Guid(pluginid));
            List<Core.Models.BusinessUnitPlugin> buplugins = core.BusinessUnitPlugins.Where(m => (m.BusinessUnitId == buid && m.PluginId != null)).ToList();
            /* Remove the plugins that are in the DB but removed by administrator */
            foreach (Core.Models.BusinessUnitPlugin buplugin in buplugins)
            {
                if (!PluginIds.Exists(m => m.Equals(buplugin.PluginId)))
                {
                    buplugin.IsDroppedYN = "Y";
                    /* Add audit entry for Plugin removal here */
                }
            }
            /* Add the plugins that do not exist */
            foreach (Guid PluginId in PluginIds)
            {
                if (buplugins.Exists(m => m.PluginId == PluginId))
                {
                    Core.Models.BusinessUnitPlugin buplugin = buplugins.First(m => m.PluginId == PluginId);
                    if (buplugin.IsDroppedYN == "Y")
                    {
                        buplugin.IsDroppedYN = "N";
                        /* Add audit entry for Plugin add here */
                    }
                }
                else
                {
                    Core.Models.BusinessUnitPlugin buplugin = new Core.Models.BusinessUnitPlugin();
                    buplugin.BusinessUnitId = buid;
                    buplugin.PluginId = PluginId;
                    buplugin.IsDroppedYN = "N";
                    core.AddToBusinessUnitPlugins(buplugin);
                    /* Add audit entry for Plugin add here */
                }
            }
            /* Changes will be saved by the calling function 
            core.SaveChanges(); */
        }
    }
}